import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/instance_manager.dart';
import 'package:time_manager/app/model/item_list_model.dart';
import 'package:time_manager/app/store/global_data.dart';
import 'package:time_manager/dict/table_dict.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:my_dart_packages/my_dart_packages.dart';
class AddTimeView extends StatelessWidget {
  AddTimeView({Key? key}) : super(key: key);
  final GlobalData store = Get.find<GlobalData>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('今日日程'),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Text('新建\n日程'),
        onPressed: () async {
          // controller.addRecord();

          Get.defaultDialog(
              title: "选择时间",
              content: TimeList(
                confirm: (item) {
                  ItemList temp = ItemList(
                      time: DateTime.now().millisecondsSinceEpoch,
                      duration: item['value'],
                      desc: '');
                  store.addItem(temp);

                  DataBaseRecord()
                      .insert(TableName.timeList.name, temp.toJson());
                },
              ));
        },
      ),
      body: Obx(() {
        return ListView(
          children: [
            // ButtonDemo(text: '测试组件'),
            Container(
              margin: const EdgeInsets.symmetric(vertical: 10),
              child: DefaultTextStyle(
                style: const TextStyle(fontSize: 16, color: Colors.blue),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('总计:${store.currentList.length}次'),
                    const SizedBox(
                      width: 20,
                    ),
                    Text('时长:${store.todayTime.toString()}分钟'),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('${(store.todayTime / 60).toStringAsFixed(2)}小时'),
                  ],
                ),
              ),
            ),
            ...store.currentList.map((it) {
              return Container(
                margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                child: Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: DefaultTextStyle(
                      style: const TextStyle(color: Colors.black, fontSize: 16),
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(TimeUtil().getLocalTime(it.time.toString())),
                            Text(it.duration.toString() + '分钟')
                          ]),
                    ),
                  ),
                ),
              );
            }),
          ],
        );
        // return ListView.builder(itemBuilder: (_, index) {
        //   return Text(index.toString());
        // });
      }),
    );
  }
}

class TimeList extends StatefulWidget {
  final Function(dynamic item) confirm;
  const TimeList({Key? key, required this.confirm}) : super(key: key);

  @override
  State<TimeList> createState() => _TimeListState();
}

class _TimeListState extends State<TimeList> {
  final timeList = [
    {"name": "10分钟", "value": 10},
    {"name": "20分钟", "value": 20},
    {"name": "30分钟", "value": 30},
    {"name": "1小时", "value": 60},
    {"name": "1.5小时", "value": 90},
    {"name": "2小时", "value": 120},
    {"name": "2.5小时", "value": 150},
    {"name": "3小时", "value": 180},
  ];
  dynamic curretItem = {"name": "10分钟", "value": 10};
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(children: [
          Wrap(
            children: timeList
                .map((it) => GestureDetector(
                      onTap: () {
                        setState(() {
                          curretItem = it;
                        });
                      },
                      child: Container(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 10, vertical: 8),
                        margin: const EdgeInsets.only(right: 10, bottom: 10),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: curretItem['value'] == it['value']
                              ? Theme.of(context).primaryColor
                              : Colors.grey[200],
                        ),
                        child: Text(
                          it['name'] as String,
                          style: TextStyle(
                              color: curretItem['value'] == it['value']
                                  ? Colors.white
                                  : Colors.black),
                        ),
                      ),
                    ))
                .toList(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(
                  onPressed: () {
                    Get.back();
                  },
                  child: const Text('取消')),
              const SizedBox(
                width: 20,
              ),
              ElevatedButton(
                  onPressed: () {
                    widget.confirm(curretItem);
                    Get.back();
                  },
                  child: const Text('确定')),
            ],
          )
        ]),
      ),
    );
  }
}
